<?php
/**
 * 武汉市中心医院中间件
 *
 *
 * @author Jack
 */

namespace Whszxyy\Controller;

class WhszxyyControlxin1Controller
{
    // SoapClient 对象
    private $soap = FALSE;

    // 记录是否为正式环境
    private $isFormal = FALSE;

    //是否开启缓存
    private static $CACHE = false;

    // 医疗机构科室列表缓存建
    private static $CACHE_INSTITUTION_CLASS_LIST = 'CACHE_INSTITUTION_CLASS_LIST';


    //static $URL_API = 'http://59.175.191.226:9192/OrderTreat/services/HosiptalManager?wsdl';
    //正式库预约挂号20170615 add by jack
    static $URL_API = 'http://192.168.100.138:9192/OrderTreat/services/HosiptalManager?wsd';

    //绑卡专用
    //static $CARD_URL_API = 'http://59.175.191.226:9192/OnlineManager/services/RegisterUserManager?wsdl';
    static $CARD_URL_API = 'http://192.168.100.138:9192/OnlineManager/services/RegisterUserManager?wsdl';//20170615 add by jack
    static $back_URL_API = "http://localhost:8091";
    //static $back_URL_API = "59.175.191.229";

    //医院id
    private $hospitalId;

    //厂商id
    private $requestId='512';
    private $requestId_Old='tcwy';  //武汉市中心医院老系统门诊缴费住院预交金授权ID

    //自助机id
    private $userId;

    /**
     * 构造函数
     */
    function __construct()
    {
        $this->hospitalId = 1;
        if (!method_exists($this, ACTION_NAME)) {
            $function = ACTION_NAME;
            A('Receive')->$function();
        }
    }


    public $hospitalKey = array(
        "1" => "7daf7603429012aeae417fa9beed9c75",
    );
    //正式地址
    //  static $URL_API = "http://125.64.9.81:8088/rmc/mobile/web";
    //  static $version = "2.2.7";
    //  private $token = "6a2967e64a994e54b533aa7942328179";
    //正式地址

    public function xinuu(){
        echo 666;
    }

    //pzh数据加密
    public function pzh_sign($content){
        //import('@.ORG.CloudHisAPIService');
        //$hisObj = new CloudHisAPIService;
        $hospitalId = 1;
        //$rsaPrivateKeyPem='http://pzh.91cure.com/bcp.keystore';
        $rsaPrivateKeyPem='http://127.0.0.1:80/bcp.keystore';
        $content = htmlspecialchars_decode($content);
        $priKey = file_get_contents($rsaPrivateKeyPem);  
        $priKey='-----BEGIN RSA PRIVATE KEY-----
MIICWwIBAAKBgQCQq3qatgsHLl4E2OKsMDYS07yp+Vdxxq6oHgdp5DmzXlt28b/u
/ezex7kNrNHWVcycaLqeX43Xp4ByO6ELjVbfP2XNhXN7pOMh5KNLbUvGP5YultKl
AMRk25HN9rjp8RHZHlS0qpR5Y5sitK3AR19VlAPKUaoslBlCaAPqGwsQkwIDAQAB
AoGARzt30eeixHVrK73S1m+ioGCLO7IfAVGPvA2VHumWE6B5LrbYB4gV8peqoXYf
mAaFCdpzUPnBsJa0IwsDP9nnFJ6EwYfvFtgihipWgx3HZ0tyLu6gNW9ORFP9RCtE
u05J8JbjYOcJP3Pwu3RQ715yoxbmAe2SzwHiXrJSUgVSsQkCQQD+YpPlkxLrGbxI
nbNAZQRO4udJIUmBt0uTNiJ8TEZ9h45aeLRYgtBy28YMBgCkBJACFuJ7MufPvwQC
4lPHJKT/AkEAkZaYCIs1A778bDvxIyGhTcUmNxTti2HEFbUFfkXXDKaxu53uxNCw
roztQD3rtGEhSGQKoO8ax0bjyIDzaX8wbQJAGsJn6Gh2PwDwC3t7VbTKpTm9NrYn
s1uCubOiIQ0tBRtzYBT+c6kfIFX6GV2wR5CrTWz22hraw8HGPD9urK4wJQJAA+V9
6CyhnJoN82+fQotx37z5CtxahzJ0pwgTP4/q84G6nATcERRsxh8rcl614jv/Qgq7
ZhdAJjk1e3ZIDXjD+QJAIF07entOK7DhmHKOXJPtgMaVdXd1QUER1NuDpQTBKTim
hjs4JBVWvXgUwPH1lZaiSoJnHrL/G3Sw3K/DJEEdKw==
-----END RSA PRIVATE KEY-----';
        $res = openssl_pkey_get_private($priKey);
        //echo $res ;die();
        openssl_sign($content, $sign, $res);
        openssl_free_key($res);  
        //$sign = base64_encode($sign); die(); 
        $sign = bin2hex($sign);
        //echo $content_jj;
        //var_dump($sign);
        //echo "<br>";
        echo $sign;
        //return $sign;  

    }

    //pzh数据加密
    public function kkkzh_sign222(){
        // echo '123';//die();
        $hospitalId = 1;
        //echo '125';die();
        $content='<input><inputxml><inputxml><yad901>tcyl_oeTsDvx_c9sJhQ3XrqxBeVsfDJxY</yad901></inputxml></inputxml></input>';
        $rsaPrivateKeyPem='http://127.0.0.1:80/bcp.keystore';
        //echo '123';die();
        $content = htmlspecialchars_decode($content);
        $priKey = file_get_contents($rsaPrivateKeyPem);  
		//echo $content;
        $priKey='-----BEGIN RSA PRIVATE KEY-----
MIICWwIBAAKBgQCQq3qatgsHLl4E2OKsMDYS07yp+Vdxxq6oHgdp5DmzXlt28b/u
/ezex7kNrNHWVcycaLqeX43Xp4ByO6ELjVbfP2XNhXN7pOMh5KNLbUvGP5YultKl
AMRk25HN9rjp8RHZHlS0qpR5Y5sitK3AR19VlAPKUaoslBlCaAPqGwsQkwIDAQAB
AoGARzt30eeixHVrK73S1m+ioGCLO7IfAVGPvA2VHumWE6B5LrbYB4gV8peqoXYf
mAaFCdpzUPnBsJa0IwsDP9nnFJ6EwYfvFtgihipWgx3HZ0tyLu6gNW9ORFP9RCtE
u05J8JbjYOcJP3Pwu3RQ715yoxbmAe2SzwHiXrJSUgVSsQkCQQD+YpPlkxLrGbxI
nbNAZQRO4udJIUmBt0uTNiJ8TEZ9h45aeLRYgtBy28YMBgCkBJACFuJ7MufPvwQC
4lPHJKT/AkEAkZaYCIs1A778bDvxIyGhTcUmNxTti2HEFbUFfkXXDKaxu53uxNCw
roztQD3rtGEhSGQKoO8ax0bjyIDzaX8wbQJAGsJn6Gh2PwDwC3t7VbTKpTm9NrYn
s1uCubOiIQ0tBRtzYBT+c6kfIFX6GV2wR5CrTWz22hraw8HGPD9urK4wJQJAA+V9
6CyhnJoN82+fQotx37z5CtxahzJ0pwgTP4/q84G6nATcERRsxh8rcl614jv/Qgq7
ZhdAJjk1e3ZIDXjD+QJAIF07entOK7DhmHKOXJPtgMaVdXd1QUER1NuDpQTBKTim
hjs4JBVWvXgUwPH1lZaiSoJnHrL/G3Sw3K/DJEEdKw==
-----END RSA PRIVATE KEY-----';
        $res = openssl_pkey_get_private($priKey);
        openssl_sign($content, $sign, $res);
        openssl_free_key($res);  
        //$sign = base64_encode($sign);  
        $sign = bin2hex($sign);
        //echo $content_jj;
        //var_dump($content_jj);
        //echo "<br>";
        echo $sign;
        return $sign;  

    }
	
    //pzh数据加密
    public function kkkzh_sign1(){
         //echo '123';//die();
        $hospitalId = 1;
        //echo '125';die();
        $content='<input><inputxml><inputxml><yad901>tcyl_oeTsDvx_c9sJhQ3XrqxBeVsfDJxY</yad901></inputxml></inputxml></input>';
        //$rsaPrivateKeyPem='http://pzh.91cure.com/bcp.keystore';
        $rsaPrivateKeyPem='http://127.0.0.1:80/bcp.keystore';
        //echo '123';die();
        $content = htmlspecialchars_decode($content);
         $priKey='-----BEGIN RSA PRIVATE KEY-----
MIICWwIBAAKBgQCQq3qatgsHLl4E2OKsMDYS07yp+Vdxxq6oHgdp5DmzXlt28b/u
/ezex7kNrNHWVcycaLqeX43Xp4ByO6ELjVbfP2XNhXN7pOMh5KNLbUvGP5YultKl
AMRk25HN9rjp8RHZHlS0qpR5Y5sitK3AR19VlAPKUaoslBlCaAPqGwsQkwIDAQAB
AoGARzt30eeixHVrK73S1m+ioGCLO7IfAVGPvA2VHumWE6B5LrbYB4gV8peqoXYf
mAaFCdpzUPnBsJa0IwsDP9nnFJ6EwYfvFtgihipWgx3HZ0tyLu6gNW9ORFP9RCtE
u05J8JbjYOcJP3Pwu3RQ715yoxbmAe2SzwHiXrJSUgVSsQkCQQD+YpPlkxLrGbxI
nbNAZQRO4udJIUmBt0uTNiJ8TEZ9h45aeLRYgtBy28YMBgCkBJACFuJ7MufPvwQC
4lPHJKT/AkEAkZaYCIs1A778bDvxIyGhTcUmNxTti2HEFbUFfkXXDKaxu53uxNCw
roztQD3rtGEhSGQKoO8ax0bjyIDzaX8wbQJAGsJn6Gh2PwDwC3t7VbTKpTm9NrYn
s1uCubOiIQ0tBRtzYBT+c6kfIFX6GV2wR5CrTWz22hraw8HGPD9urK4wJQJAA+V9
6CyhnJoN82+fQotx37z5CtxahzJ0pwgTP4/q84G6nATcERRsxh8rcl614jv/Qgq7
ZhdAJjk1e3ZIDXjD+QJAIF07entOK7DhmHKOXJPtgMaVdXd1QUER1NuDpQTBKTim
hjs4JBVWvXgUwPH1lZaiSoJnHrL/G3Sw3K/DJEEdKw==
-----END RSA PRIVATE KEY-----';
        //$priKey = file_get_contents($rsaPrivateKeyPem);
        echo  $priKey;  
        $res = openssl_pkey_get_private($priKey);
		
        openssl_sign($content, $sign, $res);
        openssl_free_key($res); 
		
        //echo  'sign123';
        //$sign = base64_encode($sign);  
        $sign = bin2hex($sign);
        //echo $content_jj;
        //var_dump($content);
        //echo "<br>";
		//echo "1wddd";
        echo $sign;
        return $sign;  

    }
    /**
     * 统一调接口方法 
     * $xml       接口传递的参数
     * $funNo     接口的方法名
     * $client    接口需要调用的webservice对象 
     * */
    function Get_service_api($xml,$funNo,$client){
        $mmkj='http://yinhai.com';
        $code='wx20170921'; //授权码yinhai@789
        //$apiUrl='http://pzhrs.91cure.com/index.php?g=Home&m=Test&a=pzh_sign';
        $apiUrl='http://127.0.0.1:442/index.php?g=Whszxyy&m=WhszxyyControlxin1&a=pzh_sign';
        $params1 = array();
        $params1['content']=$xml;
        $xmlsing=$this->PostData_jm($apiUrl,$params1);
        //echo $apiUrl;
        //echo "<br>";
        //echo $params1;
        //echo "<br>";
        //echo $xml;
        //echo "<br>";
        echo strlen($xmlsing);
        echo "<br>777";die();
        //echo strlen($xmlsing1);
        //echo "<br>";
        //dump($xmlsing1);die();
        $auth_header = array( "jybh" =>$funNo ,"systemKey" =>$code ,"source" =>$xml,"signature" =>$xmlsing  );
        $authvalues = new \SoapVar($auth_header, SOAP_ENC_OBJECT,"system",$mmkj);  
        $u = new \SoapHeader($mmkj, 'system', $authvalues);
        $client->__setSoapHeaders($u);
        $params['inputxml']=$xml;
        $suc=$client->callBusiness($params);
        $data=$suc->return;
        $tesres = $this->xml_to_array($data);
        return  $tesres;
    }

    function PostData_jm($url, $post = null) {
        dump($url);
        if (is_array($post)) {
            ksort($post);
            $content = http_build_query($post);
            $content = urldecode($content);
            echo '-999-';
            echo $content;
            $content_length = strlen($content);
            $options = array(
                'http' => array(
                    'method' => 'POST',
                    'header' =>
                    "Content-type: application/x-www-form-urlencoded\r\n"."Content-length: $content_length\r\n",
                    'content' => $content
                )
            );
            $now = time();
            print_r($options);
            $context = stream_context_create($options);
            echo '-888-';
             echo $context;
            //$res = file_get_contents($url, false, $context);
            echo '-11111-';
            //echo $res;//die();

            //$rsaPrivateKeyPem='http://pzh.91cure.com/bcp.keystore';
            $rsaPrivateKeyPem='http://172.16.8.2:442/bcp.keystore';
            $content11 = htmlspecialchars_decode($post['content']);
            $priKey = file_get_contents($rsaPrivateKeyPem);  
            $res1 = openssl_pkey_get_private($priKey);
            echo $content11;
            echo '-555-';
            echo $priKey;
            openssl_sign($content11, $sign, $res1);
            openssl_free_key($res1);  
            //$sign = base64_encode($sign);  
            $sign = bin2hex($sign);
            echo '-777-';
            echo $sign;
            echo '-666-';
            return substr(urlencode($res),9);//
            //return $res;
        }
    }

    /**
     * 数字签名
     * $url         签名地址
     * $post        签名参数
     * */
    function PostData_jm22($url, $post = null) {
        
        //echo $post['content'];
        //echo "<br>";
        if (is_array($post)) {
            ksort($post);
            $content = http_build_query($post);
            //echo $content;
            //echo "11111";
            //echo str_replace('%2F','</',$content);
           // echo "1111111";
            //echo str_replace('%3E','>',str_replace('%3C','<',str_replace('%3C%2F','></',$content)));
            //echo str_replace('%3E','>',str_replace('%3C','<',str_replace('%2F','</',$content)));
            $content = urldecode($content);
             //echo $content;
            //echo "33333333";
            //echo htmlspecialchars_decode($content);
            $content_length = strlen($content);
            echo $content_length;
            $options = array(
                'http' => array(
                    'method' => 'POST',
                    'header' =>
                    "Content-type: application/x-www-form-urlencoded\r\n"."Content-length: $content_length\r\n",
                    'content' => $content
                )
            );
            $now = time();
             print_r($options);

            $data['content']='<input><inputxml><inputxml><yad901>tcyl_oeTsDvx_c9sJhQ3XrqxBeVsfDJxY</yad901></inputxml></inputxml></input>';

             if (function_exists('file_get_contents')) {
                $file_contents = file_get_contents($url);
                echo '-bbb-';
                echo $file_contents;
            }else{
                echo '-ccc-';
            };
            if ($file_contents =='') {
                echo '<bb>dddd<bb>';
                $ch = curl_init();
                $header = "Content-type: application/x-www-form-urlencoded\r\n"."Content-length: $content_length\r\n";
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
                curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
                //curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
                //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                //curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                $tmpInfo = curl_exec($ch);
                $errorno=curl_errno($ch);
                echo '123';
                echo $tmpInfo;
                echo $errorno;
            };
            echo "<br>444<br>";
             $context = stream_context_create($options);
             echo $context;
             echo "<br>666<br>";
            $res = file_get_contents($url, false, $context);
            //var_dump($res);
            echo $url;//die();
            echo "<br>555<br>";

            $rsaPrivateKeyPem='http://127.0.0.1:442/bcp.keystore';
            $content = htmlspecialchars_decode($content);
            $priKey = file_get_contents($rsaPrivateKeyPem);  
            $res11 = openssl_pkey_get_private($priKey);
            openssl_sign($content, $sign, $res11);
            openssl_free_key($res11);  
            //$sign = base64_encode($sign);  
            $sign = bin2hex($sign);

            echo $sign;//die();
            echo "<br>888<br>";
            return substr(urlencode($res),9);//
            //return $res;
        }
    }

    function curlPost($url, $data){
        $ch = curl_init();
        $header = "Accept-Charset: utf-8";
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);
        $errorno=curl_errno($ch);
        return $tmpInfo;
    }

    /**
     * 制卡查询
     * @param 
     */
    function GetCard_Inquiries(){
        $tcylkey = C('TCYL_KEY');
        //$wx_openid  = $tcylkey.$_POST['wx_openid'];
        $wx_openid='tcyl_oeTsDv8zEhse5Hs_isEog1ncAlQ8'; //第三方ID
        $identyId='510402198306076317';    //社会保障号(身份证号码)
        $name='张帆';     //姓名
        $funNo='WZ2000';    //交易编号

        $xml="<input><inputxml><inputxml><yad901>".$wx_openid."</yad901><aac002>".$identyId."</aac002><aac003>".$name."</aac003><fwid>".$funNo."</fwid></inputxml></inputxml></input>";
        //echo $xml;//die();
        $r_url='http://172.16.0.2:20309/xtpt/services/yinHaiBusiness?wsdl';
        $client=new \SoapClient($r_url);
        $result_list=$this->Get_service_api($xml,$funNo,$client);
        //echo $xml;
        echo $result_list;die();
        $datares=$result_list['outputxml'];
        if(is_array($result_list['message'])){
            $llmessage='';  
        }else{
            $llmessage=$result_list['message']; 
        };
        if($result_list['code']=='1'){
            $returnArr['status'] = 1;
            $returnArr['errorMsg']=$llmessage;  
            $returnArr['data']['identyId']=$datares['aac002'];  //社会保障号
            $returnArr['data']['name']=$datares['aac003'];      //姓名
            $returnArr['data']['zkzt']=$datares['zkzt'];        //制卡状态
            $returnArr['data']['szztdate']=$datares['aae036'];  //所在状态时间        
            $returnArr['data']['cardno']=$datares['aaz501'];    //社保卡号
            $returnArr['data']['zkbank']=$datares['aae008'];    //制卡银行          
            $returnArr['data']['zkzbank']=$datares['aff002'];   //制卡支行
            $returnArr['data']['sbkszdq']=$datares['yae735'];   //社保卡所在地区
        }else{//格式化为标准接口模式
            $returnArr['status'] = 2;
            $returnArr['errorMsg'] = $llmessage;
        };
        dump($returnArr);
        echo json_encode($returnArr);
    }
}
?>